package com.xhsj.user.hr.system.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xhsj.user.hr.process.processDefinition.query.MapQuery;
import com.xhsj.user.hr.system.entity.RoleMenu;
import com.xhsj.user.hr.system.mapper.TbGlobalRoleMapper;
import com.xhsj.user.pms.patent.domain.OpinionFile;
import com.xhsj.user.pms.patent.domain.PatentFile;
import com.xhsj.user.pms.patent.mapper.OpinionFileMapper;
import com.xhsj.user.pms.patent.mapper.OpinionMapper;
import com.xhsj.user.pms.patent.mapper.PatentFileMapper;
import com.xhsj.user.utils.IdGen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description 上线自动化脚本工具类
 * @Author suxiaolin
 * @Date 2020/11/2 10:19
 */
@RestController
@RequestMapping("/system/test")
public class TestController {

    private static ArrayBlockingQueue<Double> queue = new ArrayBlockingQueue<>(1);

    private static Semaphore semaphore = new Semaphore(10);



    @GetMapping("/provider")
    public void provider() throws InterruptedException {
        System.err.println(Thread.currentThread());
        double random = Math.random();
        queue.put(random);
        System.err.println(Thread.currentThread().getName()+"-入队成功-"+random);
    }

    @GetMapping("/consumer")
    private void consumer() throws InterruptedException {
        System.err.println(Thread.currentThread());
        Double take = queue.take();
        System.err.println(Thread.currentThread().getName()+"-消费成功");
        System.err.println(take);

    }

    @GetMapping("/testSemaphore")
    private void testSemaphore() {

    }

    /*@Autowired
    private TbGlobalRoleMapper tbGlobalRoleMapper;

    @Autowired
    private PatentFileMapper patentFileMapper;

    @Autowired
    private OpinionFileMapper opinionFileMapper;


    @GetMapping("/updatePmsFileName")
    public void updateFileName() {
        Map<String, String> map = Maps.newHashMap();
        File file = new File("D:\\workspace\\idea_workspace\\upload\\Patent\\disclosure");
        File[] files = file.listFiles();
        for (File file1 : files) {
            File parentFile = file1.getParentFile();
            String fileName = file1.getName();
            String substring = fileName.substring(fileName.lastIndexOf("."));
            String newFileName = IdGen.uuid()+substring;
            System.err.println(fileName+"-------"+newFileName);
            File file2 = new File(parentFile + "/" + newFileName);
            file1.renameTo(file2);
            map.put(fileName,newFileName);
        }

        List<PatentFile> patentFiles = patentFileMapper.selectList(new QueryWrapper<>());
        for (PatentFile patentFile : patentFiles) {
            String fileName = patentFile.getFileName();
            if (map.containsKey(fileName)) {
                String s = map.get(fileName);
                String replace = patentFile.getUrl().replace(fileName, s);
                System.err.println(fileName+"-->"+replace);
                patentFile.setUrl(replace);
                patentFile.setDelUrl(replace);
                patentFileMapper.updateById(patentFile);
            }
        }

        System.err.println("123");
    }


    @GetMapping("/updatePmsOpinionFileName")
    public void updateOpinionFileName() {
        Map<String, String> map = Maps.newHashMap();
        File file = new File("D:\\workspace\\idea_workspace\\upload\\Patent\\opinion");
        File[] files = file.listFiles();
        for (File file1 : files) {
            File parentFile = file1.getParentFile();
            String fileName = file1.getName();
            String substring = fileName.substring(fileName.lastIndexOf("."));
            String newFileName = IdGen.uuid()+substring;
            System.err.println(fileName+"-------"+newFileName);
            File file2 = new File(parentFile + "/" + newFileName);
            file1.renameTo(file2);
            map.put(fileName,newFileName);
        }

        List<OpinionFile> patentFiles = opinionFileMapper.selectList(new QueryWrapper<>());
        for (OpinionFile patentFile : patentFiles) {
            String fileName = patentFile.getFileName();
            if (map.containsKey(fileName)) {
                String s = map.get(fileName);
                String replace = patentFile.getUrl().replace(fileName, s);
                System.err.println(fileName+"-->"+replace);
                patentFile.setUrl(replace);
                opinionFileMapper.updateById(patentFile);
            }
        }

        System.err.println("123");
    }


    @GetMapping("/updateLsmFileName")
    public void updateLsmFileName() {
        Map<String, String> map = Maps.newHashMap();
        File file = new File("D:\\workspace\\idea_workspace\\upload\\Patent\\disclosure");
        File[] files = file.listFiles();
        for (File file1 : files) {
            File parentFile = file1.getParentFile();
            String fileName = file1.getName();
            String substring = fileName.substring(fileName.lastIndexOf("."));
            String newFileName = IdGen.uuid()+substring;
            System.err.println(fileName+"-------"+newFileName);
            File file2 = new File(parentFile + "/" + newFileName);
            file1.renameTo(file2);
            map.put(fileName,newFileName);
        }

        List<PatentFile> patentFiles = patentFileMapper.selectList(new QueryWrapper<>());
        for (PatentFile patentFile : patentFiles) {
            String fileName = patentFile.getFileName();
            if (map.containsKey(fileName)) {
                String s = map.get(fileName);
                String replace = patentFile.getUrl().replace(fileName, s);
                System.err.println(fileName+"-->"+replace);
                patentFile.setUrl(replace);
                patentFile.setDelUrl(replace);
                patentFileMapper.updateById(patentFile);
            }
        }

        System.err.println("123");
    }*/


    /*@GetMapping("/testInsertRoleMenu")
    public void testInsertRoleMenu() {

        List<MapQuery> roles = tbGlobalRoleMapper.findRoles();

        List<Long> longs = Arrays.asList(129L, 117L);

        for (Long val : longs) {

            List<RoleMenu> res = Lists.newArrayList();
//            List<Long> ids = Arrays.asList(1427L, 1425L, 1424L, 1423L);
            List<Long> ids = Arrays.asList(1428L);
            for (Long id : ids) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setMenuId(id);
                roleMenu.setRoleId(val);
                res.add(roleMenu);
            }
            tbGlobalRoleMapper.saveRoleMenu(res);
        }


    }*/


    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();   //创建一个栈
        for (int i = 0; i < s.length(); i++) {
            if ((s.charAt(i) == '(') || (s.charAt(i) == '[') || (s.charAt(i) == '{')) {    // 如果是左括号，则将其放入栈内
                stack.push(s.charAt(i));
            }
            if ((s.charAt(i) == ')') || (s.charAt(i) == ']') || (s.charAt(i) == '}')) {   // 如果是右括号
                if (stack.empty()) {     //  如果栈为空，则证明括号不匹配，返回false
                    return false;
                }
                if ((stack.peek() == '(' && s.charAt(i) == ')') || (stack.peek() == '[' && s.charAt(i) == ']') || (stack.peek() == '{' && s.charAt(i) == '}') ) {     // 如果栈顶元素和下一个右括号相匹配，则将其栈顶元素出栈
                    stack.pop();
                }
            }
        }
        if (stack.empty()) {   // 遍历循环结束后，如果发现栈里为空，则证明括号匹配完毕；否则括号不匹配
            return true;
        }
        return false;
    }


    public static void main(String[] args) {

        System.out.println("Please enter a sentence include '('，')'，'{'，'}'，'['，']' ：");
        Scanner scanner = new Scanner(System.in);
        String string = scanner.nextLine();
        System.out.println("The result is：" + isValid(string));
    }

}
